﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Log;

namespace TasksLog
{
    public partial class EditForm : Form
    {
        private LogItem item = null;
        public EditForm(LogItem item)
        {
            InitializeComponent();
            using (var repository = new LogRepository(Parametrs.LogURL, Parametrs.DebugURL, null))
                if (repository.Access)
                    this.item = repository.Get(item.ID).FirstOrDefault();
            InitializeData();
        }

        private void BtCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void InitializeData()
        {
            using (var repository = new LogRepository(Parametrs.LogURL, Parametrs.DebugURL, null))
                if (repository.Access)
                {
                    var time = LogTime.Empty;
                    if (this.item.Log != null)
                        foreach (var log in this.item.Log)
                            time.Add(log.Time);

                    TBName.Text = this.item.Text;
                    DTPTime.Value = new DateTime(2000, 10, 1, time.Hour, time.Minutes, time.Second);
                    DTPDeadLine.Value = new DateTime(this.item.DeadLine.Value.Year, this.item.DeadLine.Value.Month, this.item.DeadLine.Value.Day);

                    if (DTPDeadLine.Value < DateTime.Now)
                        DTPDeadLine.CalendarForeColor = Color.Red;

                    CBStatus.SelectedIndex = (int)this.item.Status;

                    switch (this.item.Status)
                    {
                        case LogStatuses.Complite: CBStatus.ForeColor = Color.Green; break;
                        case LogStatuses.Wait: CBStatus.ForeColor = Color.Blue; break;
                        case LogStatuses.Error: CBStatus.ForeColor = Color.Red; break;
                    }

                    DTPDate.Value = this.item.Create;
                }
                else
                    MessageBox.Show("Repository access error!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private void BtSave_Click(object sender, EventArgs e)
        {
            using (var repository = new LogRepository(Parametrs.LogURL, Parametrs.DebugURL, null))
                if (repository.Access)
                {
                    var setTime = new LogTime(
                            DTPTime.Value.Hour,
                            DTPTime.Value.Minute,
                            DTPTime.Value.Second
                        );

                    var currTime = LogTime.Empty;
                    
                    if (this.item.HasLog)
                        foreach (var logDate in this.item.Log)
                            currTime.Add(logDate.Time);

                    var second = currTime.GetSecond() - setTime.GetSecond();
                    second = second < 0 ? second * -1 : second;

                    var finalTime = new LogTime(second);

                    if (this.item.HasLog && (this.item.Log.Where(x => x.Date.Value.ToShortDateString() == DateTime.Now.ToShortDateString()).Count() != 0))
                        this.item.Log.FirstOrDefault(x => x.Date.Value.ToShortDateString() == DateTime.Now.ToShortDateString()).Time.Add(finalTime);
                    else if (finalTime.GetSecond() != 0)
                        this.item.AddLog(new LogDate(DateTime.Now, this.item.Log == null ? setTime : finalTime));

                    var newitem = this.item.Dublicate();

                    if (DTPDate.Value.ToShortDateString() != newitem.Create.ToShortDateString())
                        newitem.AddChangeLog(new LogChange(
                            DateTime.Now,
                            XMLLogAttributes.Date.ToString(),
                            DTPDate.Value.ToShortDateString()
                        ));

                    if (DTPDeadLine.Value.ToShortDateString() != newitem.DeadLine.Value.ToShortDateString())
                        newitem.AddChangeLog(new LogChange(
                            DateTime.Now,
                            XMLLogAttributes.DeadLine.ToString(),
                            DTPDeadLine.Value.ToShortDateString()
                        ));

                    var compliteDate = newitem.Complite;
                    var selectStatus = (LogStatuses)CBStatus.SelectedIndex;

                    if (selectStatus != newitem.Status)
                    {
                        compliteDate = selectStatus == LogStatuses.Complite ? (DateTime?)DateTime.Now : null;
                        newitem.AddChangeLog(new LogChange(
                            DateTime.Now,
                            XMLLogAttributes.Status.ToString(),
                            selectStatus.ToString()
                        ));
                    }

                    newitem.Change(
                        TBName.Text, 
                        selectStatus, 
                        DTPDate.Value,
                        compliteDate, 
                        DTPDeadLine.Value, 
                        newitem.Log);

                    if (repository.Update(this.item, newitem))
                        this.Close();
                    else
                        MessageBox.Show("Update error!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    MessageBox.Show("Repository access error!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private void CBStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectValue = (sender as ComboBox).SelectedValue;
            switch ((LogStatuses)CBStatus.SelectedIndex)
            {
                case LogStatuses.Complite: CBStatus.ForeColor = Color.Green; break;
                case LogStatuses.Wait: CBStatus.ForeColor = Color.Blue; break;
                case LogStatuses.Error: CBStatus.ForeColor = Color.Red; break;
            }
        }
    }
}
